package org.zn.note.jdk.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Java8新特性
 * https://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
 */
public class Java8Index {

    static class Apple {
        double weight;

        Apple(double weight) {
            this.weight = weight;
        }
    }

    public static void main(String[] args) {
        int appleNumbers = 10000000;
        List<Apple> apples = new ArrayList<>();
        for (int i = 1; i <= appleNumbers; i++) {
            apples.add(new Apple(i));
        }
        // CPU低密度计算，并行反而体现不出来，也不会快
        // 只有CPU高密度计算时，比如浮点数，可能会体现出来
//        new Thread(() -> {
//            long startTime = System.currentTimeMillis();
//            List list = apples.stream().filter((Apple a) -> a.weight*0.1 > 3000).collect(Collectors.toList());
//            System.out.printf("Thread[T1] times[%d] numbers[%d]\n",
//                    System.currentTimeMillis() - startTime,
//                    list.size());
//        }).start();
        new Thread(() -> {
            long startTime = System.currentTimeMillis();
            List list = apples.parallelStream().filter((Apple a) -> a.weight > 30000).collect(Collectors.toList());
            System.out.printf("Thread[T2] times[%d] numbers[%d]\n",
                    System.currentTimeMillis() - startTime,
                    list.size());
        }).start();
    }
}
